package com.winsdom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winsdom.domain.vo.TopRoledVO;
import com.winsdom.dto.RoleManageDto;
import com.winsdom.entity.SysFatherPermission;
import com.winsdom.entity.SysPermission;
import com.winsdom.mapper.SysFatherPermissionMapper;
import com.winsdom.mapper.SysPermissionMapper;
import com.winsdom.mapper.SysRoleMapper;
import com.winsdom.service.SysFatherPremissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


@Service
public class SysFatherPremissionServiceImpl extends ServiceImpl<SysFatherPermissionMapper, SysFatherPermission> implements SysFatherPremissionService {

    @Autowired
    private SysFatherPermissionMapper sysFatherPermissionMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    @Transactional
    public List<RoleManageDto> updateRole(RoleManageDto roleManageDto) {
        List<SysPermission> list=sysPermissionMapper.selectList(null);

        if(roleManageDto.getRoleName() != null && !roleManageDto.getRoleName().equals("")) {
            sysFatherPermissionMapper.updateByRolename(roleManageDto.getId(), roleManageDto.getRoleName());
        }
        sysFatherPermissionMapper.deleteRole(roleManageDto.getId());
        for (Long permissionId : roleManageDto.getPermissionIds()) {
            for(SysPermission sysPermission: list){
                if(Objects.equals(sysPermission.getId(), permissionId)){
                    sysFatherPermissionMapper.insertRolePermission(roleManageDto.getId(), permissionId);
                    break;
                }
            }
        }
        return sysFatherPermissionMapper.selectRolePermission(roleManageDto.getId());
    }

    @Override
    public String selectRoleName(Long roleId, String roleName) {
        return sysFatherPermissionMapper.selectRoleName(roleId, roleName);
    }

    @Override
    public Long selectRoleId(Long id) {
        return sysFatherPermissionMapper.selectRoleById(id);
    }

    @Override
    public List<TopRoledVO> getListRole(){
        System.out.println(sysRoleMapper.getListRole());
        return sysRoleMapper.getListRole();
    }
}
